# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

import pymysql
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from dataob.items import FollowerItem

class DataobPipeline:

    #初始化，连接库
    def __init__(self):
        print('初始化数据库连接')
        self.conn = pymysql.connect(port=3306,
                                    host='127.0.0.1',
                                    user='root',
                                    password='root',
                                    database='data_observer',
                                    cursorclass=pymysql.cursors.DictCursor)
        self.cur = self.conn.cursor()

    def process_item(self, item, spider):

        if isinstance(item, FollowerItem):
            try:
                self.cur.execute("INSERT INTO o_follower(uid,follower)"" VALUES(%s, %s) ",
                                 (item.uid, item.follower))
                self.conn.commit()
            except Exception as e:
                print("数据库写入错误=%s" % e)

        return item

        # 结束，关闭连接
    def close_spider(self, spider):
        print('关闭数据库资源')
            # 关闭游标
        self.cur.close()
            # 关闭连接
        self.conn.close()